<?php

Yii::import('application.modules.nosey.models._base.BaseForm');

class Form extends BaseForm {

    public $form_structure;
    public $ko_data;
    public $label;
    public $content;
    public $inicio;
    public $fin;
    public $welcome;
    public $leave;
    //public $cliente_nombre_search;
    public $date_first;
    public $date_last;

    /**
     * Extend parent rules
     * @return array Array containing validation rules
     */
    public function rules() {
        return array_merge(parent::rules(), array(
            array('form_structure, ko_data', 'safe'),
            array('name', 'unique'),
            array('welcome', 'ext.YiiConditionalValidator.YiiConditionalValidator',
                'if' => array(
                    array('welcome', 'compare', 'compareValue' => '1'),
                ),
                'then' => array(
                    array('welcome_msg', 'required'),
                ),
            ),
            array('leave', 'ext.YiiConditionalValidator.YiiConditionalValidator',
                'if' => array(
                    array('leave', 'compare', 'compareValue' => '1'),
                ),
                'then' => array(
                    array('leave_msg', 'required'),
                ),
            ),
                )
        );
    }

    /**
     * Prepares create_user_id and update_user_id attributes before saving.
     * @return boolean True if is valid
     */
    protected function beforeSave() {
        if (null !== Yii::app()->user)
            $id = Yii::app()->user->id;
        else
            $id = 1;
        if ($this->isNewRecord)
            $this->create_user_id = $id;
        $this->update_user_id = $id;
        return parent::beforeSave();
    }

    /**
     * Model Behaviors
     * @return array Array containing model behaviors
     */
    public function behaviors() {
        return array(
            'CTimestampBehavior' => array(
                'class' => 'zii.behaviors.CTimestampBehavior',
                'createAttribute' => 'creation_date',
                'updateAttribute' => 'last_update_date',
                'setUpdateOnCreate' => true,
            ),
        );
    }

    /**
     * @return Form
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public static function label($n = 1) {
        return Yii::t('noseyModule.app', 'Form|Forms', $n);
    }

    public function attributeLabels() {
        return array_merge(parent::attributeLabels(), array(
            'inicio' => Yii::t('noseyModule.app', 'Desde'),
            'fin' => Yii::t('noseyModule.app', 'Hasta'),
            'welcome' => Yii::t('noseyModule.app', 'Bienvenida'),
            'leave' => Yii::t('noseyModule.app', 'Despedida'),
        ));
    }

    public function getNosey_answer() {

        $fielValue = FieldValue::model()->findAll();

        $id_asw = 0;
        $arrayNoseyAnsweres = array();
        foreach ($fielValue as $fv) {
            if ($fv->field->section->form->id == $this->id && $id_asw != $fv->answer->id) {
                $id_asw = $fv->answer->id;

//                if (Util::FormatDate($fv->answer->creation_date, "d/m/Y") <= $this->date_last &&
//                        Util::FormatDate($fv->answer->creation_date, "d/m/Y") >= $this->date_first) {
                if ($this->compararFechas(Util::FormatDate($fv->answer->creation_date, "d/m/Y"), $this->date_last) &&
                        $this->compararFechas(Util::FormatDate($fv->answer->creation_date, "d/m/Y"), $this->date_first, false)) {

                    $arrayNoseyAnsweres = array_merge($arrayNoseyAnsweres, array($fv->answer));
                } else if ($this->date_first == null && $this->date_last == null) {
                    $arrayNoseyAnsweres = array_merge($arrayNoseyAnsweres, array($fv->answer));
                }
//                var_dump(Util::FormatDate($fv->answer->creation_date, "d/m/Y") <= $this->date_first);
//                    var_dump(Util::FormatDate($fv->answer->creation_date, "d/m/Y") >= $this->date_last);
            }
        }
        return new CArrayDataProvider($arrayNoseyAnsweres);
    }

    function compararFechas($fecha1, $fecha2, $comparar = true) {
        $f1 = explode('/', $fecha1);
        $f2 = explode('/', $fecha2);
        if ($comparar) {
            if ($f1[0] <= $f2[0] && $f1[1] <= $f2[1] && $f1[2] <= $f2[2]) {
                return true;
            } else {
                return false;
            }
        } else {
            if ($f1[0] >= $f2[0] && $f1[1] >= $f2[1] && $f1[2] >= $f2[2]) {
                return true;
            } else {
                return false;
            }
        }
    }

}
